package com.chuanye.jiguangimdemo.activitys;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chuanye.jiguangimdemo.R;
import com.chuanye.jiguangimdemo.app.MyApplication;
import com.chuanye.jiguangimdemo.entity.Event;
import com.chuanye.jiguangimdemo.entity.EventType;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.callback.GetUserInfoCallback;
import cn.jpush.im.android.api.model.Conversation;
import cn.jpush.im.android.api.model.UserInfo;

/**
 * 好友验证，不是好友时的好友 详细资料页面
 */
public class GroupNotFriendActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG = "GroupNotFriendActivity";
    private Context mContext;

    private ImageView groupnotfriend_back_iv;
    private ImageView groupnotfriend_more_iv;
    private ImageView groupnotfriend_icon_iv;
    private TextView groupnotfriend_nickname_tv;
    private TextView groupnotfriend_signature_tv;//签名
    private TextView groupnotfriend_additionalmsg_tv;//附加消息
    private TextView groupnotfriend_username_tv;

    private TextView groupnotfriend_gender_tv;
    private TextView groupnotfriend_birthday_tv;
    private TextView groupnotfriend_address_tv;
    private Button seafridetail_addfriend_btn;//加好友
    private Button seafridetail_sendmessage_btn;//发消息


    private String mUserName;
    private UserInfo mUserInfo;
    private String mMyName;
    private String mNickName;
    private String mAvatarPath;
    private LinearLayout groupnotfriend_nick_ll;
    private TextView groupnotfriend_nick_tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_not_friend);
        mContext = this;

        initView();
        initData();
    }

    private void initView() {
        //返回
        groupnotfriend_back_iv = findViewById(R.id.groupnotfriend_back_iv);
        groupnotfriend_back_iv.setOnClickListener(this);
        //更多
        groupnotfriend_more_iv = findViewById(R.id.groupnotfriend_more_iv);
        groupnotfriend_more_iv.setOnClickListener(this);
        //用户头像
        groupnotfriend_icon_iv = findViewById(R.id.groupnotfriend_icon_iv);
        //头像下的昵称 或备注名
        groupnotfriend_nickname_tv = findViewById(R.id.groupnotfriend_nickname_tv);
        //签名
        groupnotfriend_signature_tv = findViewById(R.id.groupnotfriend_signature_tv);
        //附加消息
        groupnotfriend_additionalmsg_tv = findViewById(R.id.groupnotfriend_additionalmsg_tv);
        //用户名
        groupnotfriend_username_tv = findViewById(R.id.groupnotfriend_username_tv);
        //昵称
        groupnotfriend_nick_ll = findViewById(R.id.groupnotfriend_nick_ll);
        groupnotfriend_nick_tv = findViewById(R.id.groupnotfriend_nick_tv);
        //性别
        groupnotfriend_gender_tv = findViewById(R.id.groupnotfriend_gender_tv);
        //生日
        groupnotfriend_birthday_tv = findViewById(R.id.groupnotfriend_birthday_tv);
        //地区
        groupnotfriend_address_tv = findViewById(R.id.groupnotfriend_address_tv);

        //加好友
        seafridetail_addfriend_btn = findViewById(R.id.seafridetail_addfriend_btn);
        seafridetail_addfriend_btn.setOnClickListener(this);

        //发消息
        seafridetail_sendmessage_btn = findViewById(R.id.seafridetail_sendmessage_btn);
        seafridetail_sendmessage_btn.setOnClickListener(this);
    }


    private void initData() {

        mUserName = getIntent().getStringExtra(MyApplication.TARGET_ID);
        //理由
        String reason = getIntent().getStringExtra("reason");
        if (reason == null) {
            groupnotfriend_additionalmsg_tv.setVisibility(View.GONE);
        } else {
            groupnotfriend_additionalmsg_tv.setVisibility(View.VISIBLE);
            groupnotfriend_additionalmsg_tv.setText("附加消息: " + reason);
        }
        JMessageClient.getUserInfo(mUserName, new GetUserInfoCallback() {
            @Override
            public void gotResult(int responseCode, String responseMessage, UserInfo info) {
                if (responseCode == 0) {
                    mUserInfo = info;
                    File avatar = info.getAvatarFile();
                    if (avatar != null) {
                        mAvatarPath = avatar.getAbsolutePath();
                        groupnotfriend_icon_iv.setImageBitmap(BitmapFactory.decodeFile(mAvatarPath));
                    } else {
                        groupnotfriend_icon_iv.setImageResource(R.drawable.rc_default_portrait);
                    }
                    String noteName = info.getNotename();
                    mNickName = info.getNickname();
                    //有备注 有昵称
                    groupnotfriend_username_tv.setText(mUserName);
                    if (!TextUtils.isEmpty(noteName) && !TextUtils.isEmpty(mNickName)) {
                        groupnotfriend_nick_ll.setVisibility(View.VISIBLE);
                        groupnotfriend_nick_tv.setText(mNickName);
                        groupnotfriend_nickname_tv.setText("备注名: " + noteName);
                    }
                    //没有备注 有昵称
                    else if (TextUtils.isEmpty(noteName) && !TextUtils.isEmpty(mNickName)) {
                        groupnotfriend_nick_ll.setVisibility(View.GONE);
                        groupnotfriend_nickname_tv.setText("昵称: " + mNickName);
                    }
                    //有备注 没有昵称
                    else if (!TextUtils.isEmpty(noteName) && TextUtils.isEmpty(mNickName)) {
                        groupnotfriend_nick_ll.setVisibility(View.VISIBLE);
                        groupnotfriend_nick_tv.setText(info.getNickname());
                        groupnotfriend_nickname_tv.setText("备注名: " + noteName);
                    }
                    //没有备注名 没有昵称
                    else {
                        groupnotfriend_nick_ll.setVisibility(View.GONE);
                        groupnotfriend_nickname_tv.setText("用户名: " + mUserName);
                    }
                    //签名
                    groupnotfriend_signature_tv.setText(info.getSignature());
                    //性别
                    if (info.getGender() == UserInfo.Gender.male) {
                        groupnotfriend_gender_tv.setText("男");
                    } else if (info.getGender() == UserInfo.Gender.female) {
                        groupnotfriend_gender_tv.setText("女");
                    } else {
                        groupnotfriend_gender_tv.setText("未知");
                    }
                    //生日
                    groupnotfriend_birthday_tv.setText(getBirthday(info));
                    //地址
                    groupnotfriend_address_tv.setText(info.getRegion());
                }

            }
        });

        UserInfo myInfo = JMessageClient.getMyInfo();
        mMyName = myInfo.getNickname();
        if (TextUtils.isEmpty(mMyName)) {
            mMyName = myInfo.getUserName();
        }
    }


    public String getBirthday(UserInfo info) {
        long birthday = info.getBirthday();
        Date date = new Date(birthday);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        return dateFormat.format(date);
    }

    @SuppressLint("WrongConstant")
    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()){
            case R.id.groupnotfriend_back_iv://返回
                finish();
                break;
            case R.id.groupnotfriend_more_iv://更多
                intent.setClass(GroupNotFriendActivity.this, NotFriendSettingActivity.class);
                intent.putExtra("notFriendUserName", mUserName);
                startActivity(intent);
                break;
            case R.id.seafridetail_addfriend_btn://加好友
                if (mUserInfo.isFriend()) {
                    Toast.makeText(GroupNotFriendActivity.this, "对方已经是你的好友",Toast.LENGTH_SHORT).show();
                } else {
                    intent.setClass(GroupNotFriendActivity.this, VerificationActivity.class);
                    //对方信息
                    intent.putExtra("detail_add_friend", mUserName);
                    intent.putExtra("detail_add_nick_name", mNickName);
                    intent.putExtra("detail_add_avatar_path", mAvatarPath);
                    //自己的昵称或者是用户名
                    intent.putExtra("detail_add_friend_my_nickname", mMyName);
                    intent.setFlags(1);
                    startActivity(intent);
                }
                break;
            case R.id.seafridetail_sendmessage_btn://发消息
                intent.setClass(GroupNotFriendActivity.this, ChatActivity.class);
                //创建会话
                intent.putExtra(MyApplication.TARGET_ID, mUserInfo.getUserName());
                intent.putExtra(MyApplication.TARGET_APP_KEY, mUserInfo.getAppKey());
                String notename = mUserInfo.getNotename();
                if (TextUtils.isEmpty(notename)) {
                    notename = mUserInfo.getNickname();
                    if (TextUtils.isEmpty(notename)) {
                        notename = mUserInfo.getUserName();
                    }
                }
                intent.putExtra(MyApplication.CONV_TITLE, notename);
                Conversation conv = JMessageClient.getSingleConversation(mUserInfo.getUserName(), mUserInfo.getAppKey());
                //如果会话为空，使用EventBus通知会话列表添加新会话
                if (conv == null) {
                    conv = Conversation.createSingleConversation(mUserInfo.getUserName(), mUserInfo.getAppKey());
                    EventBus.getDefault().post(new Event.Builder()
                            .setType(EventType.createConversation)
                            .setConversation(conv)
                            .build());
                }
                startActivity(intent);
                break;
        }
    }
}
